<template>
  <div id="userLoginPage">
    <h2 class="title">时光小馆：客官登录一下噻！</h2>
    <div class="desc">欢迎来到时光小馆，让我们在时光的海洋中谱写我们自己的故事吧！</div>
    <a-form :model="formState" name="basic" autocomplete="off" @finish="handleSubmit">
      <a-form-item
        label="账号："
        name="userAccount"
        :rules="[{ required: true, message: '喂喂喂，输入账号噻！' }]"
      >
        <a-input v-model:value="formState.userAccount" placeholder="客官，输下账号噻" />
      </a-form-item>

      <a-form-item
        label="密码："
        name="userPassword"
        :rules="[
          { required: true, message: '喂喂喂，密码也要输入呀。' },
          { min: 8, message: '密码长度不能小于8位哦' },
        ]"
      >
        <a-input-password v-model:value="formState.userPassword" placeholder="客官，输下密码噻" />
      </a-form-item>
      <div class="tips">
        啥？没有账号？
        <router-link to="/user/register">点击注册</router-link>
      </div>
      <a-form-item>
        <a-button type="primary" html-type="submit" style="width: 100%">登录</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { useLoginUserStore } from '@/stores/user'
import { useRouter } from 'vue-router'
import { userLoginUsingPost } from '@/api/userController'
import { message } from 'ant-design-vue'

const formState = reactive<API.UserLoginRequest>({
  userAccount: '',
  userPassword: '',
})

const router = useRouter()
const loginUserStore = useLoginUserStore()

const handleSubmit = async (values: API.UserLoginRequest) => {
  const res = await userLoginUsingPost(values)
  if (res.data.code === 0 && res.data.data) {
    await loginUserStore.fetchLoginUser()
    message.success('登录成功')
    router.push({
      path: '/',
      replace: true,
    })
  } else {
    message.error('登录失败：' + res.data.message)
  }
}
</script>
<style scoped>
#userLoginPage {
  /* 设置登录页面最大宽度为360px */
  max-width: 360px;
  /* 水平居中 */
  margin: 0 auto;
}

.title {
  /* 标题文字居中 */
  text-align: center;
  /* 标题底部边距16px */
  margin-bottom: 16px;
}

.desc {
  /* 描述文字居中 */
  text-align: center;
  /* 描述文字颜色为#bbb */
  color: #bbb;
  /* 描述文字底部边距16px */
  margin-bottom: 16px;
}

.tips {
  /* 提示区域底部边距16px */
  margin-bottom: 16px;
  /* 提示文字颜色为#bbb */
  color: #bbb;
  /* 提示文字大小为13px */
  font-size: 13px;
  /* 提示文字右对齐 */
  text-align: right;
}
</style>
